﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace WallpaperEngine
{
    public class NotifyIcon
    {
        private const string IconPath = "WallpaperEngine.ico";

        private ContextMenu menu;

        private Utility.NotifyIcon notifyIcon;

        private MenuItem transparentMenu;

        private Wallpaper _wallpaper;

        private MenuItem chosenWallpaper;

        private MenuItem startWithSystem;

        private MenuItem allowHook;

        private MenuItem closeWallpaper;

        public NotifyIcon(Wallpaper wallpaper)
        {
            _wallpaper = wallpaper;
            transparentMenu = new MenuItem("任务栏透明化", ((s, e) =>
            {
                if (s is MenuItem m)
                {
                    Configuration.Config.AllowShellTrayTransparency = !m.Checked;
                    m.Checked = !m.Checked;
                }
            }));

            startWithSystem = new MenuItem("开机自启", ((s, e) =>
            {
                if (s is MenuItem m)
                {
                    if (m.Checked)
                    {
                        Utility.ShortCut.Delete();
                    }
                    else
                    {
                        Utility.ShortCut.Create();
                    }
                    m.Checked = !m.Checked;
                }
            }));

            allowHook = new MenuItem("允许鼠标钩子", ((s, e) =>
            {
                if (s is MenuItem m)
                {
                    Configuration.Config.AllowMouseHook = !m.Checked;
                    m.Checked = !m.Checked;
                }
            }));

            closeWallpaper = new MenuItem("关闭壁纸", ((s, e) =>
            {
                _wallpaper.Close();
                _wallpaper.ToOldWallpaper();
                Configuration.Config.CurrentWallpaper = null;
                if (chosenWallpaper != null)
                {
                    chosenWallpaper.Checked = false;
                    chosenWallpaper = null;
                }
                closeWallpaper.Enabled = false;
            }));

            var Wallpapers = new List<MenuItem>();
            foreach (FileInfo File in new DirectoryInfo(PathManager.DataFolder).GetFiles())
            {
                Wallpapers.Add(new MenuItem(File.Name, ((s, e) =>
                {
                    _wallpaper.Close();
                    ToggleChosenWallpaper(s as MenuItem);
                    _wallpaper.SetWallpaper(File.FullName);
                    closeWallpaper.Enabled = true;
                })));
            }

            Wallpapers.Add(new MenuItem("-"));

            Wallpapers.Add(new MenuItem("随机", ((s, e) =>
            {
                _wallpaper.Close();
                var target = Wallpapers[new Random().Next(Wallpapers.Count)];
                ToggleChosenWallpaper(target);
                _wallpaper.SetWallpaper(PathManager.DataFolder + $"{target.Text}");
                closeWallpaper.Enabled = true;
            })));

            transparentMenu.Checked = Configuration.Config.AllowShellTrayTransparency;
            startWithSystem.Checked = Utility.ShortCut.Exists();
            allowHook.Checked = Configuration.Config.AllowMouseHook;
            if (Configuration.Config.CurrentWallpaper != null && Utility.Common.IsPowerOnline())
            {
                chosenWallpaper = Wallpapers.Find(menu => menu.Text == Configuration.Config.CurrentWallpaper);
                chosenWallpaper.Checked = true;
            }
            else
            {
                closeWallpaper.Enabled = false;
            }

            menu = new ContextMenu(new MenuItem[]
                {
                    new MenuItem("改变壁纸", Wallpapers.ToArray()),

                    new MenuItem("-"),

                    startWithSystem,

                    transparentMenu,

                    allowHook,

                    closeWallpaper,

                    new MenuItem("退出", ((s, e) =>
                    {
                        System.Windows.Application.Current.Shutdown();
                    }))
                });
            notifyIcon = new Utility.NotifyIcon(menu, IconPath);
        }
        
        public void Close()
        {
            notifyIcon.Close();
        }

        private void ToggleChosenWallpaper(MenuItem sender)
        {
            if (chosenWallpaper != null)
            {
                chosenWallpaper.Checked = false;
            }
            chosenWallpaper = sender;
            chosenWallpaper.Checked = true;
        }
    }
}
